<?php

// +------------------------------------------------------
// | Name: 中向荣  新闻热点api
// +------------------------------------------------------
// | Version: V1
// +------------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +------------------------------------------------------
// | Time: 2021-05-06 15:40
// +------------------------------------------------------
declare(strict_types=1);

namespace app\api\controller\v1;

use app\common\common\IndexController;
use app\common\model\Artice as ArticeModel;
use app\common\model\Cate as CateModel;
use app\common\model\HotSearch as HotSearchModel;
use app\common\model\Collect as UserCollectModel;
use app\common\model\Gbook as GbookModel;
use app\common\model\User as UserModel;
use app\common\model\PushMsg as PushMsgModel;
use app\common\model\StationMessage as StationMessageModel;
use think\facade\Db;

/**
 * @xk 新闻热点api
 */
class News extends IndexController
{
    /**
     * @xk 获取新闻热点列表
     *
     * @method getNewsList
     *
     * @param ArticeModel $articeModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-06T15:41:47+080
     */
    public function getNewsList(ArticeModel $articeModel)
    {
        $param = request()->param();
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $limit = isset($param['limit']) ? (int) $param['limit'] : 30;
        $cate_id = isset($param['cate_id']) ? $param['cate_id'] : 'tj';
        $parent_id = isset($param['parent_id']) ? $param['parent_id'] : '0';
        $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
        $keywords = isset($param['keywords']) ? $param['keywords'] : '';
        $condition = [];
        if ('tj' == $cate_id) {
            $condition[] = ['a.cate_str', 'like', '%,'.$parent_id.',%'];
            $condition[] = ['a.is_tj', '=', 1]; // 获取推荐
        } elseif ('gz' == $cate_id) {
            $condition[] = ['a.cate_id','in',$cate_arr];
        } else {
            $condition[] = ['a.cate_id', '=', $cate_id];
        }
        $condition[] = ['a.status','=',1];
        if ($keywords != '') {
            $condition[] = ['a.title|a.content','like','%'.$keywords.'%'];
        }
        $code = 200;
        $msg = '请求成功';
        $result_count = $articeModel->alias('a')->where($condition)->count();
        if ($result_count <= ($page - 1) * $limit) {
            $result_code = 203;
            $result_msg = '没有更多数据了！';
            $resultData = [];
            $resultData['list'] = [];
            $resultData['page'] = $page;
            $this->result($code, $msg, $result_code, $result_msg, $resultData, $result_count);
            exit();
        }
        $list = $articeModel
            ->alias('a')
            ->where($condition)
            ->field('a.`artice_id`,a.`title`,a.`linkurl`,a.`update_time` as show_time,a.`hits`,a.`picurl`,a.`fileurl`')
            ->withAttr('picurl', function ($value, $data) {
                if ($value) {
                    return getShowUrl($value);
                }
            })
            ->withAttr('fileurl', function ($value, $data) {
                if ($value) {
                    return getShowUrl($value);
                }
            })
            ->withAttr('show_time', function ($value, $data) {
                return chanageDateString($value, 2);
            })
            ->order('order_id desc')
            ->page($page, $limit)
            ->select()
            ->toArray();
        $result_code = 200;
        $result_msg = '加载成功';
        $resultData = [];
        $resultData['list'] = $list;
        $resultData['page'] = $page + 1;
        $this->result($code, $msg, $result_code, $result_msg, $resultData, $result_count);
    }
    /**
     * @xk 获取新闻详情
     *
     * @method getNewsShow
     *
     * @param ArticeModel $articeModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-06T15:57:20+080
     */
    public function getNewsShow(ArticeModel $articeModel)
    {
        $param = request()->param();
        $artice_id = isset($param['artice_id']) ? $param['artice_id'] : 0; // 图文id
        $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
        $code = 200;
        $msg = '请求成功';
        $resultData = [];
        if ($artice_id <= 0) {
            $result_code = 404;
            $result_msg = '请选择需要查看的热点id';
            $resultData = [];
            $this->result($code, $msg, $result_code, $result_msg, $resultData);
        }
        // 增加点击量
        $res = $articeModel->where([['artice_id', '=', $artice_id]])->inc('hits')->update();
        // 获取详情
        $info = $articeModel
                ->field('`artice_id`,`cate_id`,`title`,`content`,`hits`,`picurl`,`fileurl`,`cate_id`,`update_time` as show_time')
                ->withAttr('picurl', function ($value, $data) {
                    if ($value) {
                        return getShowUrl($value);
                    }
                })
                ->withAttr('fileurl', function ($value, $data) {
                    return getShowUrl($value);
                })
                ->withAttr('content', function ($value, $data) {
                    return chanageContent($value);
                })
                ->withAttr('show_time', function ($value, $data) {
                    return chanageDateString($value, 2);
                })
                ->find($artice_id);
        $tj_list = [];
        $info = $info ? $info->toArray() : [];
        $result_code = 200;
        $result_msg = '加载成功';
        $info['tel'] = config('web_config.tel');
        $resultData['info'] = $info;
        $resultData['tj_list'] = $tj_list;
        $resultData['tel_time'] = config('web_config.tel_time');
        $resultData['online_time'] = config('web_config.online_time');
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 内容web展示
     *
     * @method show
     *
     * @param ArticeModel $articeModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-14T10:27:00+080
     */
    public function getWebshow(ArticeModel $articeModel)
    {
        $param = request()->param();
        $artice_id = isset($param['artice_id']) ? $param['artice_id'] : 0;
        $info = $articeModel
          ->where([
              ['artice_id', '=', $artice_id],
          ])
          ->field('title,content')
          ->withAttr('content', function ($value, $data) {
              return chanageContent($value);
          })
          ->find();
        if (is_null($info)) {
            return "<div style='text-align: center;padding: 30px 0;'>暂无详情</div>";
        }

        return web_page($info['title'], $info['content']);
    }

    /**
     * @xk 获取收益说明
     *
     * @method getWithDrawalRule
     *
     * @param ArticeModel $articeModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-05-14T10:34:05+080
     */
    public function getWithDrawalRule(ArticeModel $articeModel)
    {
        $info = $articeModel
              ->where([['cate_id', '=', 3]])
              ->field('`artice_id`,`content`,`picurl`')
              ->withAttr('picurl', function ($value, $data) {
                  if ($value) {
                      return getShowUrl($value);
                  }
              })
              ->withAttr('content', function ($value, $data) {
                  if ($value) {
                      return chanageContent($value);
                  }
              })
              ->find();
        $info = $info ? $info->toArray() : [];
        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '加载成功';
        $resultData = [];
        $resultData['info'] = $info;
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 获取转增规则
     *
     * @method getGiveRule
     * @return [type]      [description]
     * @Time   2021-10-29
     * @City   青岛
     * @Author xkyinzhe    <zhonghuaxinxing@sina.cn>
     */
    public function getGiveRule(ArticeModel $articeModel)
    {
        $info = $articeModel
            ->where([['cate_id', '=', 4]])
            ->field('`artice_id`,`content`,`picurl`')
            ->withAttr('picurl', function ($value, $data) {
                if ($value) {
                    return getShowUrl($value);
                }
            })
            ->withAttr('content', function ($value, $data) {
                if ($value) {
                    return chanageContent($value);
                }
            })
            ->find();
        $info = $info ? $info->toArray() : [];
        $code = 200;
        $msg = '请求成功';
        $result_code = 200;
        $result_msg = '加载成功';
        $resultData = [];
        $resultData['info'] = $info;
        $resultData['min_give_money'] = config('web_config.min_give_money'); // 最低转账金额
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 消息推送记录
     * @method                                 getPushMsg
     * @param                                  PushMsgModel $pushMsgModel  [description]
     * @return                                 [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                        <zhonghuaxinxing@sina.cn>
     * @City：                                  青岛
     * @Time：2022-02-28Tfalse10:false15:02+080
     */
    public function getPushMsg(PushMsgModel $pushMsgModel)
    {
        $param = request()->param();
        $page = isset($param['page']) ? (int) $param['page'] : 1; // 当前页
        $limit = isset($param['limit']) ? (int) $param['limit'] : 20; // 请求数量
        $is_page = isset($param['is_page']) ? $param['is_page'] : 1; // 是否进行分页
        $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
        $condition = [];
        $condition[] = ['pu.user_sn','=',$user_sn];
        $code = 200;
        $msg = '请求成功';
        $resultData = [];
        try {
            $result_count = $pushMsgModel->alias('pu')->where($condition)->count(); // 请求总数量
            if ($result_count <= ($page - 1) * $limit) {
                $resultData['list'] = [];
                $resultData['page'] = $page;
                throw new \think\Exception("没有更多数据了", 203);
            }
            $list = $pushMsgModel
              ->alias('pu')
              ->where($condition)
              ->field('pu.`push_msg_id`,pu.`user_sn`,pu.`title`,pu.`content`,pu.`create_time`,pu.`is_read`')
              ->order(['pu.is_read asc','pu.create_time desc'])
              ->page($page, $limit)
              ->select()
              ->toArray();
            $result_code = 200;
            $result_msg = '加载成功';
            $resultData = [];
            $resultData['list'] = $list;
            $resultData['page'] = $page + 1;
        } catch (\Exception $e) {
            $result_code = 200;
            if ($e->getCode() == 203) {
                $result_code = 203;
            }
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
    /**
     * @xk 获取消息推送详情
     * @method                                      getPushMsgInfo
     * @param                                       PushMsgModel $pushMsgModel  [description]
     * @return                                      [type] [description]
     * @Version：V5.0
     * @Author：xkyinzhe                             <zhonghuaxinxing@sina.cn>
     * @City：                                       青岛
     * @Time：2022-02-28Tfalse10:false21:false59+080
     */
    public function getPushMsgInfo(PushMsgModel $pushMsgModel){
      $param = request()->param();
      $push_msg_id = isset($param['push_msg_id']) ? (int) $param['push_msg_id'] : 0; // 当前页
      $user_sn = isset($param['user_sn']) ? $param['user_sn'] : '';
      $code = 200;
      $msg = '请求成功';
      $resultData = [];
      try {
          $info = $pushMsgModel->find($push_msg_id);
          $pushMsgModel->find($push_msg_id)->save(['is_read'=>1,'read_time'=>date('Y-m-d H:i:s')]);
          $resultData['info'] = $info;
          $result_code = 200;
          $result_msg = '加载成功';
      } catch (\Exception $e) {
          $result_code = 200;
          if ($e->getCode() == 203) {
              $result_code = 203;
          }
          $result_msg = $e->getMessage();
      }
      $this->result($code, $msg, $result_code, $result_msg, $resultData);
    }
}
